#! /usr/local/bin/python
#-*- coding: utf-8 -*-

"""Interface graphique pour la sauvegarde des Highscores.
"""

"""
Minesweeper 3K. Original implementation of the classic game of mines
for Python 3.
Copyright (C) 2010  Cedric Bonhomme

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.

Minesweeper 3K  Copyright (C) 2010  Cedric Bonhomme
This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
This is free software, and you are welcome to redistribute it
under certain conditions; type `show c' for details.
"""

__author__ = "Jerome Hussenet, Cedric Bonhomme"
__date__ = "$Date: 2010/02/27 $"
__copyright__ = "Copyright (c) 2010 Jerome Hussenet, Copyright (c) 2010 Cedric Bonhomme"
__license__ = "GPL 3"

from tkinter import *
import tkinter.messagebox
import score
import string

class SauvegardeGui(object):
    """Interface graphique pour la sauvegarde des scores.
    """
    def __init__(self, temps, niveau, parent):
        """Initialisation de l'interface.
        """
        self.frame = Toplevel()
        self.frame.title(_('Félicitation !'))
        self.frame.resizable(height = False, width = False)

        self.nom = ""
        self.temps = temps
        self.niveau = niveau

        Label(self.frame, text = _('Vous pouvez figurer dans les meilleurs scores !\nEntrez votre nom :'))\
            .grid(row=1, column = 0, padx = 50, pady = 10)

        self.nomEntry = Entry(self.frame)
        self.nomEntry.grid(row = 2, column = 0, pady = 2)
        self.nomEntry.focus_set()

        Button(self.frame, text=_("OK"), \
            command = self.test).grid(row = 4, column = 0, padx = 50, pady = 10)

        ## fixe le focus sur la fenêtre (obligatoire pour Windows)
        self.frame.focus_set()
        ## Force les évènements à aller sur cette fenêtre
        self.frame.grab_set()
        ## Pour que la fenêtre reste au-dessus de sa fenêtre parente
        self.frame.transient(parent)
        ## Affiche la fenêtre et attend sa fermeture
        self.frame.wait_window(self.frame)

    def test(self):
        """Vérifie la saisie.
        """
        try:
            self.nom = self.nomEntry.get()
        except:
            tkinter.messagebox.showerror(_('Erreur'), _("Erreur de saisie."))
            self.nomEntry.delete(0, END)
            return
        if self.nom == "":
            tkinter.messagebox.showerror(_('Erreur'), _("Saisissez votre nom."))
            return
        if len(self.nom) > 15:
            tkinter.messagebox.showerror(_('Erreur'), _("Le nom ne doit pas dépasser 15 caractères."))
            self.nomEntry.delete(0, END)
            return
        self.sauvegarde()

    def sauvegarde(self):
        """Sauvegarde le score.
        """
        save = score.Score()
        save.addPersonne(self.niveau, self.nom, str(self.temps))
        save.write()
        self.frame.destroy()

if __name__ == '__main__':
    # Point d'entrée en exécution
    sauv = SauvegardeGui(1001, "debutant")